{ "cells": [ { "cell_type": "markdown", "id": "intro-loops", "metadata": {}, "source": [ "# Introduction to Loops in Python" ] }, { "cell_type": "markdown", "id": "concept-loops", "metadata": {}, "source": [ "## Step 1: What are Loops?\n", "Loops are an essential concept in programming that allow you to repeat a block of code multiple times. This repetition is incredibly useful when you want to process a collection of items or perform an operation until a condition is met.\n", "In Python, there are two main types of loops:\n", "- **`for` loop**: Used when you want to iterate over a sequence (like a list, tuple, or string) or perform an action a specific number of times.\n", "- **`while` loop**: Used when you want the loop to continue running as long as a certain condition remains `True`." ] }, { "cell_type": "markdown", "id": "types-loops", "metadata": {}, "source": [ "### The Two Main Types of Loops in Python\n", "- **`for` loop**: This loop works by iterating over a sequence (like a list or string) and performing the block of code for each item.\n", "- **`while` loop**: This loop runs as long as a given condition is `True`. It is commonly used when you don't know how many times the loop should run beforehand and depends on some condition." ] }, { "cell_type": "markdown", "id": "for-loop", "metadata": {}, "source": [ "## Step 2: The `for` Loop\n", "A `for` loop is generally used when you know the number of iterations or need to loop over a sequence of items like a list, tuple, or string.\n", "Example of a `for` loop iterating over a list of numbers:" ] }, { "cell_type": "code", "execution_count": null, "id": "code-for-loop", "metadata": {}, "outputs": [], "source": [ "numbers = [1, 2, 3, 4, 5]\n", "# The for loop will go through each element in the numbers list\n", "for num in numbers:\n", " print(num) # Output: 1 2 3 4 5" ] }, { "cell_type": "markdown", "id": "while-loop", "metadata": {}, "source": [ "## Step 3: The `while` Loop\n", "A `while` loop continues to run as long as a specified condition is `True`. This type of loop is useful when the number of iterations is not known in advance and depends on some condition.\n", "Example of a `while` loop that counts from 0 to 4:" ] }, { "cell_type": "code", "execution_count": null, "id": "code-while-loop", "metadata": {}, "outputs": [], "source": [ "count = 0\n", "# The loop runs as long as count is less than 5\n", "while count < 5:\n", " print(count) # Output: 0 1 2 3 4\n", " count += 1 # Increment count by 1 after each iteration" ] }, { "cell_type": "markdown", "id": "break-continue", "metadata": {}, "source": [ "## Step 4: Using `break` and `continue`\n", "You can control the flow of a loop with the `break` and `continue` statements:\n", "- `break`: Immediately exits the loop.\n", "- `continue`: Skips the current iteration and moves to the next iteration of the loop." ] }, { "cell_type": "code", "execution_count": null, "id": "code-break-continue", "metadata": {}, "outputs": [], "source": [ "for num in range(10):\n", " if num == 5:\n", " break # Stops the loop when num is 5\n", " if num == 3:\n", " continue # Skips number 3\n", " print(num) # Output: 0 1 2 4" ] }, { "cell_type": "markdown", "id": "nested-loops", "metadata": {}, "source": [ "## Step 5: Nested Loops\n", "A nested loop is a loop inside another loop. This is useful when you need to perform a task that involves multiple sequences, like iterating over a list of lists.\n", "Example of nested loops iterating over a pair of sequences:" ] }, { "cell_type": "code", "execution_count": null, "id": "code-nested-loops", "metadata": {}, "outputs": [], "source": [ "for i in range(3):\n", " for j in range(2):\n", " print(f'({i}, {j})') # Output: (0,0) (0,1) (1,0) (1,1) (2,0) (2,1)" ] }, { "cell_type": "markdown", "id": "exercise-loops", "metadata": {}, "source": [ "## Step 6: Practical Exercises\n", "Now it's time to practice what you've learned! Try to solve the following exercises." ] }, { "cell_type": "markdown", "id": "455e845c", "metadata": {}, "source": [ "### Exercise 1: Print Numbers 1 to 10\n", "Write a program that prints the numbers from 1 to 10 using a `for` loop. The output should look like: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10." ] }, { "cell_type": "markdown", "id": "d5976fe7", "metadata": {}, "source": [ "### Exercise 2: Countdown\n", "Write a program that uses a `while` loop to print a countdown from 10 to 1." ] }, { "cell_type": "markdown", "id": "exercise-3", "metadata": {}, "source": [ "### Exercise 3: Sum of Numbers\n", "Write a program that calculates the sum of all numbers from 1 to 100 using a `for` loop. Output the sum at the end." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.x" } }, "nbformat": 4, "nbformat_minor": 5 }